import pandas as pd
import plotly as py
import plotly.express as px
py.offline.init_notebook_mode()
data = {'Country':['China','India','Germany','France'],
'2021':[2000,1000,500,200],
'2022':[3000,500,5000,10]
}
df = pd.DataFrame(data)
df
| Country | 2021 | 2022 | |
|---|---|---|---|
| 0 | China | 2000 | 3000 |
| 1 | India | 1000 | 500 |
| 2 | Germany | 500 | 5000 |
| 3 | France | 200 | 10 |
numb = (df['Country']=='India').index
df.loc[1]
Country India 2021 1000 2022 500 Name: 1, dtype: object
fig = px.bar(df,x='Country',y=['2021','2022'],title = 'Representation of 2022 population',barmode = 'group')
fig.show()
from dash import Dash,html,dcc,Input,Output
from jupyter_dash import JupyterDash
app = JupyterDash()
app.layout = html.Div([
html.Div([
html.H1(children = 'Population vs Country for the year 2022',style = {'textAlign':'Center','color':'black','background-color':'coral','font-size':'20px','font-family':'Times New Roman'})
]),
html.Div([
html.Label('Countries'),
dcc.RadioItems(['China','India','Germany','France'],'India',id = 'Country1',labelStyle = {'display':'block'}, style = {'font-family':'Arial'}),
html.Label('Year',style={'margin-left':'100px'}),
dcc.Checklist(['2021','2022'],['2021'],id = 'Year1', labelStyle = {'display':'block'},style = {'font-family':'Arial'}),
],style = {'display':'flex','font-family':'Arial','background-color':'Burlywood'}
),
html.Div([dcc.Graph(id = 'Bar_graph',figure = fig,style = {'height':250,'width':450,'display':'inline-block'})],style={'margin-left':'200px'})
],style = {'background-color':'Black'}
)
@app.callback(Output(component_id = 'Bar_graph',component_property = 'figure'),
Input(component_id = 'Country1',component_property = 'value'),
Input(component_id = 'Year1',component_property = 'value'))
def get_graph(count,y1):
df1 = df[df['Country']==count]
fig1 = px.bar(df1,x='Country',y=y1,title = 'Representation of 2022 population',barmode = 'group')
fig1.update_layout(autosize = False,width = 500,height = 300,xaxis_title = 'Country',yaxis_title = 'Population',plot_bgcolor='LightBlue',legend_title = 'Years')
fig1.update_layout(font = dict(family = 'Times New Roman',size = 15, color = 'red'))
fig1.update_traces(width = .05)
fig1.update_layout(legend = dict(font = dict(family = 'Arial',size =10,color = 'black'),bgcolor = 'LightSteelBlue',bordercolor ='black'))
return fig1
if __name__ =='__main__':
app.run_server(mode='inline',port = 6789)